package punctuatedEquilibrium;

public class Phenotype {

	private static final double	SUMMAND_TO_GET_MEAN_OF_RANDOM_TO_ZERO	= -0.5;
	private Trait[]				traits;

	public Phenotype(Trait[] averageTraitValues) {
		traits = averageTraitValues;
	}

	public void changeWithSmallVariability() {
		for (Trait trait : traits) {
			double randomChange = SUMMAND_TO_GET_MEAN_OF_RANDOM_TO_ZERO
					+ Math.random();
			trait.add(randomChange);
		}
	}

	public Phenotype duplicate() {
		return createDuplicate();
	}

	public Phenotype duplicateApplyingSmallVariability() {
		Phenotype duplicatedPhenotype = createDuplicate();
		duplicatedPhenotype.changeWithSmallVariability();
		return duplicatedPhenotype;
	}

	public Double[] getArrayOfVariables() {
		int arrayLength = traits.length;
		Double[] traitValues = new Double[arrayLength];
		for (int i = 0; i < arrayLength; i++) {
			traitValues[i] = traits[i].reportValue();
		}
		return traitValues;
	}

	public boolean isEquivalent(Phenotype phenotypeToCompareAgainst) {
		Double[] variablesToCompareAgainst = phenotypeToCompareAgainst
				.getArrayOfVariables();
		Double[] variablesOfThisObject = getArrayOfVariables();
		if (variablesOfThisObject.length == 0
				&& variablesToCompareAgainst.length == 0) {
			return true;
		}
		if (variablesOfThisObject.length != variablesToCompareAgainst.length) {
			return false;
		}
		for (int i = 0; i < variablesOfThisObject.length; i++) {
			if (variablesOfThisObject[i]
					.compareTo(variablesToCompareAgainst[i]) != 0) {
				return false;
			}
		}
		return true;
	}

	private Phenotype createDuplicate() {
		Trait[] duplicateTraits = ArrayDuplicator.duplicate(traits);
		Phenotype duplicatePhenotype = new Phenotype(duplicateTraits);
		return duplicatePhenotype;
	}

}
